package xyz.ddlnt.user.usr.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xyz.ddlnt.commonutil.result.Result;
import xyz.ddlnt.commonutil.util.HttpUtils;
import xyz.ddlnt.model.dto.PageDTO;
import xyz.ddlnt.model.dto.PostFavRecordDTO;
import xyz.ddlnt.model.dto.UserContentInteractionDTO;
import xyz.ddlnt.model.vo.PostRecordVO;
import xyz.ddlnt.serviceusrclient.client.PostFavRecordFeignClient;

import java.util.List;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/25 17:42
 */
@Slf4j
@Tag(name = "帖子收藏接口测试")
@RequestMapping("/post_fav_record")
@RestController
public class PostFavRecordController {
    @Resource
    private PostFavRecordFeignClient postFavRecordFeignClient;

    @Operation(summary = "判断用户是否收藏了帖子")
    @GetMapping("/isFavorite/{postId}")
    Result<Boolean> isFavorite(@PathVariable Long postId, HttpServletRequest request) {
        UserContentInteractionDTO userContentInteractionDTO = UserContentInteractionDTO.builder()
                .userId(HttpUtils.getId(request))
                .id(postId)
                .build();
        return postFavRecordFeignClient.isFavorite(userContentInteractionDTO);
    }

    @Operation(summary = "添加收藏")
    @PostMapping("/addFav")
    Result<Boolean> addFav(@RequestBody PostFavRecordDTO postFavRecordDTO, HttpServletRequest request) {
        postFavRecordDTO.setUserId(HttpUtils.getId(request));
        return postFavRecordFeignClient.addFav(postFavRecordDTO);
    }

    @Operation(summary = "取消收藏")
    @DeleteMapping("/removeFav")
    Result<Boolean> removeFav(@RequestBody UserContentInteractionDTO userContentInteractionDTO, HttpServletRequest request) {
        userContentInteractionDTO.setUserId(HttpUtils.getId(request));
        return postFavRecordFeignClient.removeFav(userContentInteractionDTO);
    }


    @Operation(summary = "获取收藏列表")
    @PostMapping("/getFavList")
    Result<List<PostRecordVO>> getFavList(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if (pageDTO.getId() == null) {
            pageDTO.setId(HttpUtils.getId(request));
        }
        return postFavRecordFeignClient.getFavList(pageDTO);
    }
}
